package ar.uba.fi.taller2.cmmiassistant.frontend.evaluacion.representacion;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.HashMap;
import java.util.Map;

import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingConstants;

import ar.uba.fi.taller2.cmmiassistant.controlador.Controlador;
import ar.uba.fi.taller2.cmmiassistant.modelo.cmmi.Madurez;
import ar.uba.fi.taller2.cmmiassistant.modelo.cmmi.ModeloProcesos;
import ar.uba.fi.taller2.cmmiassistant.modelo.evaluacion.Escalonada;
import ar.uba.fi.taller2.cmmiassistant.modelo.evaluacion.Evaluacion;
import ar.uba.fi.taller2.cmmiassistant.modelo.evaluacion.Representacion;
import ar.uba.fi.taller2.cmmiassistant.vista.Estado;
import ar.uba.fi.taller2.cmmiassistant.vista.Vista;

public class MostrandoNivelMadurez extends MostrandoNivel {

    private static final long serialVersionUID = -2534265410168443033L;

    private ButtonGroup buttonGroupNivel = null;

    private Map<Madurez, JRadioButton> radios;

    private JPanel panelNiveles = null;

    /**
     * This is the default constructor
     */
    public MostrandoNivelMadurez(Vista contexto) {
        super(contexto);
        initialize();
    }

    /**
     * This method initializes this
     * 
     * @return void
     */
    private void initialize() {

        this.setLayout(new GridBagLayout());

        GridBagConstraints constraintsLabel = new GridBagConstraints();
        constraintsLabel.gridx = 0;
        constraintsLabel.gridy = 0;
        constraintsLabel.insets = new Insets(5, 5, 5, 5);
        constraintsLabel.fill = GridBagConstraints.HORIZONTAL;

        this.add(this.crearLabelCampo("nivel de madurez"), constraintsLabel);

        GridBagConstraints constraintsNiveles = new GridBagConstraints();
        constraintsNiveles.gridx = 1;
        constraintsNiveles.gridy = 0;
        constraintsNiveles.insets = new Insets(5, 5, 5, 5);
        constraintsNiveles.fill = GridBagConstraints.BOTH;
        constraintsNiveles.weightx = 1.0;

        this.add(getPanelNiveles(), constraintsNiveles);

        this.addCompletePanel(this);
    }

    @Override
    public Estado getEstado() {

        return Estado.SELECCIONANDO_NIVEL_MADUREZ;
    }

    public ButtonGroup getButtonGroupNivel() {

        if (this.buttonGroupNivel == null) {

            this.buttonGroupNivel = new ButtonGroup();
        }

        return this.buttonGroupNivel;
    }

    /**
     * This method initializes panelNiveles
     * 
     * @return javax.swing.JPanel
     */
    private JPanel getPanelNiveles() {
        if (panelNiveles == null) {
            panelNiveles = new JPanel();
        }
        return panelNiveles;
    }

    public void setControlador(Controlador controlador) {

        super.setControlador(controlador);

        ModeloProcesos modeloProcesos = this.getModelo().getModeloProcesos();

        /* remueve todos los niveles */
        this.getPanelNiveles().removeAll();
        this.getPanelNiveles().setLayout(new GridBagLayout());

        for (Madurez madurez : modeloProcesos.getNiveles()) {

            this.addRadioButtonNivel(madurez);
        }

        this.cargar();
    }

    private void cargar() {

        Evaluacion evaluacion = this.getModelo().getEvaluacionSeleccionada()
                .getValor();

        if (evaluacion != null) {

            Representacion representacion = evaluacion.getRepresentacion();
            if ((representacion != null)
                    && (representacion instanceof Escalonada)) {

                JRadioButton radio = this.getRadios().get(
                        ((Escalonada) representacion).getMadurez());

                if (radio != null) {

                    radio.setSelected(true);
                }
            }

        }
    }

    private void addRadioButtonNivel(final Madurez madurez) {

        JRadioButton radioButton = new JRadioButton();

        radioButton.setText(madurez.getNombre());
        radioButton.setHorizontalAlignment(SwingConstants.LEFT);
        radioButton.setEnabled(false);

        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridy = madurez.getNivel();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.anchor = GridBagConstraints.WEST;
        gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new Insets(2, 2, 2, 2);
        gridBagConstraints.gridwidth = 1;
        gridBagConstraints.gridheight = 1;
        gridBagConstraints.weightx = 1.0;

        this.getPanelNiveles().add(radioButton, gridBagConstraints);
        this.getButtonGroupNivel().add(radioButton);
        this.getRadios().put(madurez, radioButton);

    }

    public Map<Madurez, JRadioButton> getRadios() {

        if (this.radios == null) {

            this.radios = new HashMap<Madurez, JRadioButton>();
        }

        return this.radios;
    }

    public void activarSeleccion() {
        
        Madurez madurez = this.getRepresentacion().getMadurez();

        if (madurez != null) {
            
            JRadioButton radio = this.getRadios().get(madurez);
            
            if (radio != null) {
                radio.setSelected(true);
            }
        }
    }

    public Escalonada getRepresentacion() {
        
        return (Escalonada) super.getRepresentacion();
    }

    protected Class<? extends Representacion> getClaseRepresentacion() {
        
        return Escalonada.class;
    }

}
